package br3akr.DeadlyDinnerParty.Server.Engine;

import java.util.TimerTask;

import br3akr.DeadlyDinnerParty.Server.Actions.Action;
import br3akr.DeadlyDinnerParty.Server.Actions.TimerAction;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;

public class TimedActionTask extends TimerTask {
	Session session = null;
	Action action = null;
	
	public TimedActionTask(Session session, Action action) {
		if (session == null) {
			throw new IllegalArgumentException("A session must be provided.");
		}
		
		if (action == null) {
			throw new IllegalArgumentException("An action must be provided.");
		}
		
		if (!(action instanceof TimerAction)) {
			throw new IllegalArgumentException("The action must implement the TimerAction interface.");
		}
		
		this.session = session;
		this.action = action;
	}
	
	@Override
	public void run() {
		((TimerAction)action).nextStep();
		try {
			Server.world.broadcastAction(action.doAction(null, session));
		} catch (DeadlyDinnerPartyException e) {
			session.buildClientUpdate(e.getMessage());
		}
	}
}
